package com.magnificent.atlantis.client.ui.planner;

import com.magnificent.atlantis.client.data.configuration.*;
import com.magnificent.atlantis.client.data.alert.AlertsContainer;
import com.magnificent.atlantis.client.data.message.MessagesContainer;
import com.magnificent.atlantis.client.data.settings.SettingsContainer;
import com.magnificent.atlantis.client.data.task.creator.AbstractCreatorTask;
import com.magnificent.atlantis.client.data.task.planner.AbstractPlannerTask;
import com.magnificent.atlantis.client.data.task.planner.BasicPlannerTask;
import com.magnificent.atlantis.client.data.task.planner.SimpleMeetingPlannerTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * User: Babanin
 * Date: 13.02.2009
 * Time: 14:07:04
 */
public class PlannerData {
    private Date programStartDate;

    private AlertsContainer alertsContainer;
    private SettingsContainer settingsContainer;
    private MessagesContainer messagesContainer;
    private ConfigurationContainer configurationContainer;

    public PlannerData() {
        alertsContainer = new AlertsContainer();
        settingsContainer = new SettingsContainer();
        messagesContainer = new MessagesContainer();
        configurationContainer = new ConfigurationContainer();
    }

    public PlannerData(AlertsContainer alertsContainer, SettingsContainer settingsContainer,
                       MessagesContainer messagesContainer, ConfigurationContainer configurationContainer) {
        this.alertsContainer = alertsContainer;
        this.settingsContainer = settingsContainer;
        this.messagesContainer = messagesContainer;
        this.configurationContainer = configurationContainer;

        programStartDate = new Date();
    }

    public void addConiguration(FileConfiguration fileConfiguration) {
        AbstractConfiguration configuration;
        if (fileConfiguration.isServerConfiguration()) {
            configuration = new ServerConfiguration();

            ((ServerConfiguration) configuration).setPort(fileConfiguration.getPort());
            ((ServerConfiguration) configuration).setHost(fileConfiguration.getHost());
        } else {
            configuration = new LocalConfiguration();
        }

        configurationContainer.getConfigurations().add(configuration);

        int id = getLastId(configurationContainer.getConfigurations());
        configuration.setId(id);
        configuration.setName(fileConfiguration.getName());
        configuration.setDescription(fileConfiguration.getDescription());

        List<Date> dates = new ArrayList<Date>();
        List<AbstractCreatorTask> tasks = fileConfiguration.getFileTasks();

        int[] indexes = new int[tasks.size() + 1];
        Arrays.fill(indexes, -1);

        int lastTaskId = getLastTaskId(configurationContainer.getDataTasks());

        for (AbstractCreatorTask abstractCreatorTask : tasks) {

            AbstractPlannerTask abstractPlannerTask;
            if (abstractCreatorTask.isBasicTask()) {
                abstractPlannerTask = new BasicPlannerTask(abstractCreatorTask);

            } else {
                abstractPlannerTask = new SimpleMeetingPlannerTask(abstractCreatorTask);
            }

            Date startDate = new Date();
            int dateId = abstractPlannerTask.getDependencyTaskId() - 1;
            if (dateId >= 0 && dateId < dates.size()) {
                startDate = dates.get(dateId);
                startDate = new Date(startDate.getTime() + abstractPlannerTask.getDependencyTaskTime());
            } else {
                if (dates.size() > 0) {
                    startDate = dates.get(dates.size() - 1);
                }
            }

            dates.add(startDate);
            abstractPlannerTask.reInitializeStartDate(startDate);

            abstractPlannerTask.setConfigurationId(id);

            indexes[abstractPlannerTask.getId()] = lastTaskId;
            abstractPlannerTask.setId(lastTaskId++);

            if (abstractPlannerTask.getDependencyTaskId() != -1) {
                abstractPlannerTask.setDependencyTaskId(indexes[abstractPlannerTask.getDependencyTaskId()]);
            }

            if (abstractPlannerTask.getDependencyTasks().size() != 0) {
                List<Integer> newDependency = new ArrayList<Integer>();

                for (Integer oldId : abstractPlannerTask.getDependencyTasks()) {
                    newDependency.add(indexes[oldId]);
                }

                abstractPlannerTask.setDependencyTasks(newDependency);
            }

            configurationContainer.getDataTasks().add(abstractPlannerTask);
        }
    }


    private static int getLastTaskId(List<AbstractPlannerTask> plannerTasks) {
        int id = -1;

        for (AbstractPlannerTask plannerTask : plannerTasks) {
            if (plannerTask.getId() > id) {
                id = plannerTask.getId();
            }
        }

        return id + 1;
    }

    private static int getLastId(List<AbstractConfiguration> localConfigurations) {
        int id = -1;

        for (AbstractConfiguration localConfiguration : localConfigurations) {
            if (localConfiguration.getId() > id) {
                id = localConfiguration.getId();
            }
        }

        return id + 1;
    }

    public AlertsContainer getAlertsContainer() {
        return alertsContainer;
    }

    public MessagesContainer getMessagesContainer() {
        return messagesContainer;
    }

    public SettingsContainer getSettingsContainer() {
        return settingsContainer;
    }

    public ConfigurationContainer getConfigurationContainer() {
        return configurationContainer;
    }

    public Date getProgramStartDate() {
        return programStartDate;
    }
}
